﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

public partial class terminate_account : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        decimal amount = decimal.Parse(tbAmount.Text);
        string strType = "FIXED DEPOSTI PAYMENT";
        string strTo = tbTo.Text;
        String fixed_acc = "";



        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(strConnectionString);

        

        try
        {

            using (SqlCommand command = new SqlCommand("SELECT accountNo FROM FIXED_DEPOSIT WHERE (status='Terminating')", myConnection))
            {
                myConnection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        fixed_acc = reader[0].ToString();

                    }
                }
            }

            SqlCommand myCommandd = new SqlCommand("INSERT INTO SUMMARY(transactionDate, transactionType, transactionStatus, accountNo, transactionAmount)" + "VALUES(@date, @type, @status, @accNo, @transactionAmount)", myConnection);

            myCommandd.Parameters.AddWithValue("@date", DateTime.Now);
            myCommandd.Parameters.AddWithValue("@type", strType);
            myCommandd.Parameters.AddWithValue("@status", "SUCCESS");
            myCommandd.Parameters.AddWithValue("@accNo",fixed_acc);
            myCommandd.Parameters.AddWithValue("@transactionAmount", 0 - amount);
            myCommandd.ExecuteNonQuery();



            SqlCommand myCommandi = new SqlCommand("INSERT INTO SUMMARY(transactionDate, transactionType, transactionStatus, accountNo, transactionAmount)" + "VALUES(@date, @type, @status, @accNo, @transactionAmount)", myConnection);

            myCommandi.Parameters.AddWithValue("@date", DateTime.Now);
            myCommandi.Parameters.AddWithValue("@type", strType);
            myCommandi.Parameters.AddWithValue("@status", "SUCCESS");
            myCommandi.Parameters.AddWithValue("@accNo", tbTo.Text);
            myCommandi.Parameters.AddWithValue("@transactionAmount", amount);
            myCommandi.ExecuteNonQuery();

            myConnection = DBManager.getSqlConnection();
            SqlCommand myCommandp = new SqlCommand("update [SAVING_ACCOUNT] SET [balance] = balance+" + amount + " WHERE [accountNo] = " + tbTo.Text, myConnection);
            myConnection.Open();
            myCommandp.Parameters.AddWithValue("@balance", amount);
            myCommandp.Parameters.AddWithValue("@account", tbTo.Text);
            myCommandp.ExecuteNonQuery();

            myConnection = DBManager.getSqlConnection();
            SqlCommand myCommandc = new SqlCommand("DELETE FROM FIXED_DEPOSIT WHERE accountNo =" + fixed_acc, myConnection);
            myConnection.Open();
            myCommandc.ExecuteNonQuery();

            myConnection.Close();

            AccountsManager am = new AccountsManager();
            String custID = am.getCustIdFromAccount(tbTo.Text);

            Alert alert = new Alert();
            alert.addAlertApplication(custID, "Your Fixed Deposit Account  " + fixed_acc+ " has been TERMINATED successfully.", "");
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            Response.Write(ex);
        }
        finally
        {
            myConnection.Close();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Withdraw sucessfully, account has been terminated');window.location='terminate_account.aspx';", true);
           
        }
    }

   
   
}